package com.andy.controller.interceptor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登录请求直接放行
        if("/user/login".equals(request.getRequestURI())) return true;
        if("/user/loginHome".equals(request.getRequestURI())) return true;

        String sessionUsername = (String)request.getSession().getAttribute("session_username");
        if(StringUtils.isBlank(sessionUsername)){
            response.sendRedirect("/user/loginHome");//跳到登录页面
            return false;
        }
        return true;
    }
}
